layui.use(['layer','table','jquery','form','laydate'],function(){
    let layer, table, $, form, layda;
    layer = layui.layer;
    table = layui.table;
    $ = layui.jquery;
    form = layui.form;

    layda = layui.laydate;
    //前后若干天可选，这里以7天为例
//,'laydata'
//     laydate写成laydata了

    let tabInstance = table.render({
        elem : "#opptunityList",
        url : "/sales-opportunity/search",
        page : true,
        id : "list",
        cols : [[
            {field:'id',title:'编号',width:80,sort:true,fixed:'left'},
            // {field:'opportunitySource',title:'机会来源'},
            {field:'sname',title:'客户名称'},
            // {field:'successProbability',title:'成功机率'},
            {field:'outline',title:'概要'},
            {field:'contacts',title:'联系人'},
            // {field:'contactsId',title:'联系人编号'},
            {field:'contactsTel',title:'联系电话'},
            // {field:'opportunityDesc',title:'机会描述'},
            // {field:'assignId',title:'联系方式'},
            // {field:'assignName',title:'指派人名称'},
            // {field:'foundId',title:'指派人编号'},
            // {field:'foundName',title:'创建人名称'},

            {field:'creationTime',title:'创建时间'},
            {title:'操作',field:"right",width:250,align:"center",toolbar:"#tool"}
        ]]
    });

    form.on('submit(search)',function(data){
        tabInstance.reload({
            where:data.field
        });
    });

    //添加
    $(".add_btn").click(function(){
        add_edit();
    });
    layda.render({
        elem: '#pointTime'
        , min: 0
        , max: 7
    });
    function add_edit(edit){
        layer.open({
            title:"添加销售机会",
            type:2,
            content:"addChance.jsp",
            area:['50%','70%'],
            skin:"layui-layer-molv",
            btn:['确定', '关闭'],

            // success: function(layero, index){
            //     let body = layer.getChildFrame("body",index);
            //     //获取选择框数据
            //     $.post("/sales-opportunity/add",function(data){
            //         console.log(data.data.depts);
            //         data.data.depts.forEach(function(dept){
            //             body.find(".dept").append(`<option value='${dept.deptId}'>${dept.name}</option>`);
            //         });
            //         form.render('select');
            //     });

                // if(edit){
                //     console.log(edit);
                //     body.find(".source").val(edit.opportunitySource);
                //     // body.find(".sex input[value="+edit.sex+"]").prop("checked","checked");
                //     body.find(".sname").val(edit.sname);
                //     // body.find(".dept option[value="+edit.deptId+"]").prop("selected","selected");
                //     body.find(".probability").val(edit.successProbability);
                //     body.find(".outline").val(edit.outline);
                //     body.find(".contacts").val(edit.contacts);
                //     body.find(".contactsid").val(edit.contactsId);
                //     body.find(".contactstel").val(edit.contactsTel);
                //     body.find(".opportunityDesc").val(edit.tel);
                // }
            // },
            yes: function(index, layero){
                let body = layer.getChildFrame("body",index);
                let data = {};
                body.find("#editForm").serializeArray().forEach(function(item){
                    data[item.name] = item.value;
                });
                $.post('/sales-opportunity/add',data,function(result){
                    layer.msg(result.msg);
                    setTimeout(function(){
                        layer.close(index);
                        parent.location.reload();
                    },300);
                });
            }
        });
    }

    //刷新单选框radio，解决单选框不显示的问题
    layui.use('form',function(){
        var form = layui.form;
        form.render('radio');
    });
    // layda.render({
    //     elem: '#pointTime'
    //     ,type: 'datetime'
    //     ,done: function(value, date, endDate){
    //         console.log(value); //得到日期生成的值，如：2017-08-18
    //         // console.log(date); //得到日期时间对象：{year: 2017, month: 8, date: 18, hours: 0, minutes: 0, seconds: 0}
    //         // console.log(endDate); //得结束的日期时间对象，开启范围选择（range: true）才会返回。对象成员同上。
    //     }
    // });


    //监听工具条
    table.on('tool(opportunityList)',function(obj){
        switch(obj.event){
            case 'point':
                layer.open({
                    type: 2,
                    content: 'point.jsp',
                    skin: 'layui-layer-molv',
                    title: "指派用户",
                    area: ["50%", "70%"],
                    btn: ['确定', '关闭'],
                    success:function (layero,index) {

                        // console.log(obj.data.editAble);
                        let body = layer.getChildFrame('body',index);
                        console.log(obj);
                        body.find(".id").val(obj.data.id);
                        body.find(".opportunitySource").val(obj.data.opportunitySource);
                        // body.find(".sex input[value="+edit.sex+"]").prop("checked","checked");
                        body.find(".sname").val(obj.data.sname);
                        // body.find(".dept option[value="+edit.deptId+"]").prop("selected","selected");
                        body.find(".successProbability").val(obj.data.successProbability);
                        body.find(".outline").val(obj.data.outline);
                        body.find(".contacts").val(obj.data.contacts);
                        body.find(".contactsId").val(obj.data.contactsId);
                        body.find(".contactsTel").val(obj.data.contactsTel);
                        body.find(".opportunityDesc").val(obj.data.opportunityDesc);
                        body.find(".assignName").val(obj.data.assignName);
                        body.find(".creationTime").val(obj.data.creationTime);
                        body.find(".foundName").val(obj.data.foundName );
                        body.find(".pointTime").val(obj.data.pointTime);
                        form.render();
                    },
                    yes: function(index, layero){
                        let body = layer.getChildFrame("body",index);
                        let data = {};
                        body.find("#editForm").serializeArray().forEach(function(item){
                            data[item.name] = item.value;
                        });
                        $.post('/sales-opportunity/edit',data,function(result){
                            layer.msg(result.msg);
                            setTimeout(function(){
                                layer.close(index);
                                parent.location.reload();
                            },300);
                        });

                    }

                });
                break;
            case 'edit':
                layer.open({
                    type: 2,
                    content: 'edit.jsp',
                    skin: 'layui-layer-molv',
                    title: "修改用户",
                    area: ["50%", "70%"],
                    btn: ['确定', '关闭'],
                    success:function (layero,index) {

                        // console.log(obj.data.editAble);
                        let body = layer.getChildFrame('body',index);
                            console.log(obj);
                            body.find(".id").val(obj.data.id);
                            body.find(".opportunitySource").val(obj.data.opportunitySource);
                            body.find(".sname").val(obj.data.sname);
                            body.find(".successProbability").val(obj.data.successProbability);
                            body.find(".outline").val(obj.data.outline);
                            body.find(".contacts").val(obj.data.contacts);
                            body.find(".contactsId").val(obj.data.contactsId);
                            body.find(".contactsTel").val(obj.data.contactsTel);
                            body.find(".opportunityDesc").val(obj.data.opportunityDesc);
                            body.find(".assignName").val(obj.data.assignName);
                            body.find(".creationTime").val(obj.data.creationTime);
                            body.find(".foundName").val(obj.data.foundName );
                            body.find(".pointTime").val(obj.data.pointTime);
                            form.render();
                    },
                    yes: function(index, layero){
                        let body = layer.getChildFrame("body",index);
                        let data = {};
                        body.find("#editForm").serializeArray().forEach(function(item){
                            data[item.name] = item.value;
                        });
                        $.post('/sales-opportunity/edit',data,function(result){
                            layer.msg(result.msg);
                            setTimeout(function(){
                                layer.close(index);
                                parent.location.reload();
                            },500);
                        });

                    }

                });
                break;
            case 'del':
                layer.confirm("你确定要删除【"+obj.data.sname+"】吗？",{icon: 3, title:'提示',skin:"layui-layer-molv",anim: 4},function(index){
                    $.post('/sales-opportunity/delete/'+obj.data.id,function (data) {
                        tabInstance.reload();
                        layer.msg(data.msg);
                    })
                });
                break;
        }
    });



});